package hu.berczi.projects.LifeGame;

import hu.berczi.projects.LifeGame.Creatures.Creature;
import hu.berczi.projects.LifeGame.Creatures.Fox;
import hu.berczi.projects.LifeGame.Creatures.Grass;
import hu.berczi.projects.LifeGame.Creatures.Hunter;
import hu.berczi.projects.LifeGame.Creatures.Mushroom;
import hu.berczi.projects.LifeGame.Creatures.Rabbit;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Iterator;

public class LifeGame {

	final private double MAX_X = 100;
	final private double MAX_Y = 100;

	private ArrayList<Creature> creaturesOnPlayGrund;

	public LifeGame() {
		this.creaturesOnPlayGrund = new ArrayList<Creature>();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		LifeGame lifeGame = new LifeGame();
		for (int i = 0; i < 100; i++) {
			double rnd = Math.random();
			String type;
			if (rnd < 0.3) {
				type = "fox";
			} else if (rnd < 0.6) {
				type = "vulture";
			} else if (rnd < 0.9) {
				type = "rabbit";
			} else {
				type = "hunter";
			}
			Creature a = lifeGame.getACreature(type);
			a.setCoord(lifeGame.pickAPlaceOnTheBoard());
			lifeGame.creaturesOnPlayGrund.add(a);
			System.out.println("Creature '" + a.getName() + "' added");
			a.print();

			ArrayList<Creature> creaturesAround = lifeGame
					.getCreaturesAround(a);

			StringBuffer out;
			if (creaturesAround.size() == 0) {
				out = new StringBuffer(" No creatures in the seerange.\n");
			} else {
				out = new StringBuffer(" Creatures around of it in a range of "
						+ a.getSeeRange() + ":\n");
				for (Iterator<Creature> iterator = creaturesAround.iterator(); iterator
						.hasNext();) {
					Creature creature = (Creature) iterator.next();
					out.append("  " + creature.getName());
					out.append("\t(" + creature.getCoord().getX() + ","
							+ creature.getCoord().getY() + "), distance: ");
					out.append(creature.getCoord().distance(a.getCoord()));
					if (a.isEnemy(creature)) {
						out.append("\tDanger, RUN!!");
					} else if (a.isSpoil(creature)) {
						out.append("\tCould be eaten...");
					}
					out.append("\n");

				}

			}

			System.out.println(out.toString());
		}
	}

	private Creature getACreature(String type) {
		Creature aNewCreature = null;
		if (type.equalsIgnoreCase("fox")) {
			aNewCreature = new Fox();
		} else if (type.equalsIgnoreCase("rabbit")) {
			aNewCreature = new Rabbit();
		} else if (type.equalsIgnoreCase("grass")) {
			aNewCreature = new Grass();
		} else if (type.equalsIgnoreCase("hunter")) {
			aNewCreature = new Hunter();
		} else if (type.equalsIgnoreCase("mushroom")) {
			aNewCreature = new Mushroom();
		} else if (type.equalsIgnoreCase("vulture")) {
			aNewCreature = new Mushroom();
		} else {
			System.err.println("There are no creature in the game '" + type
					+ "'");
		}

		return aNewCreature;
	}

	private Point2D.Double pickAPlaceOnTheBoard() {
		Point2D.Double aCoord;
		aCoord = new Point2D.Double();
		aCoord.setLocation(Math.random() * MAX_X, Math.random() * MAX_Y);

		return aCoord;

	}

	private ArrayList<Creature> getCreaturesAround(Creature creature) {
		ArrayList<Creature> crs = new ArrayList<Creature>();
		for (Creature aCreature : creaturesOnPlayGrund) {
			// Is in seerange of creature
			if (!aCreature.equals(creature)) {
				if (aCreature.getCoord().distance(creature.getCoord()) <= creature
						.getSeeRange()) {
					crs.add(aCreature);
				}
			}
		}

		return crs;

	}
}
